package com.fanhao.dllo.justforyou.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.android.volley.toolbox.ImageLoader;

import com.android.volley.toolbox.NetworkImageView;
import com.fanhao.dllo.justforyou.R;
import com.fanhao.dllo.justforyou.view.MyImageView;
import com.fanhao.dllo.justforyou.entity.FoodShowResult;
import com.fanhao.dllo.justforyou.volley.GetVolleyDatasUtil;
import com.fanhao.dllo.justforyou.volley.VolleySingleton;

import java.util.List;

/**
 * Created by dllo on 15/10/29.
 */
public class FoodShowAdapter extends RecyclerView.Adapter<
      FoodShowAdapter.FoodShowViewHolder>{
     private List<FoodShowResult.ResultEntity> result;

    private Context context;
//    private ImageLoader imageLoader;

    public void FoodShowAdapter(Context context) {
        this.context = context;
//        imageLoader = volleySingleton.getImageLoader();
//        imageLoader = VolleySingleton.getInstance().getImageLoader();
    }

    public void addData(List<FoodShowResult.ResultEntity> datas) {
        result = datas;
        notifyDataSetChanged();
    }

    public void loadData(List<FoodShowResult.ResultEntity> datas) {
        result.addAll(datas);
        notifyDataSetChanged();
    }


    public class FoodShowViewHolder extends RecyclerView.ViewHolder {
        private ImageView headIv;
        private NetworkImageView foodIv;
        private TextView nameTv,levelTv,commentTv, placenameTv,
                        foodnameTv,addressTv,phoneTv,timeTv,dateTv;

        public FoodShowViewHolder(View v) {
            super(v);
            headIv = (MyImageView)v.findViewById(R.id.headIv);
            foodIv = (NetworkImageView) v.findViewById(R.id.foodIv);

            nameTv = (TextView)v.findViewById(R.id.nameTv);
            levelTv = (TextView)v.findViewById(R.id.levelTv);
            commentTv = (TextView)v.findViewById(R.id.commentTv);
            placenameTv = (TextView)v.findViewById(R.id.placenameTv);
            foodnameTv = (TextView)v.findViewById(R.id.foodnameTv);
            addressTv = (TextView)v.findViewById(R.id.addressTv);
            phoneTv = (TextView)v.findViewById(R.id.phoneTv);
            timeTv = (TextView)v.findViewById(R.id.timeTv);
            dateTv = (TextView)v.findViewById(R.id.dateTv);
        }
    }

    @Override
    public int getItemCount() {

        return result != null && result.size() > 0
                ? result.size() : 0;
    }

    @Override
    public void onBindViewHolder(FoodShowViewHolder holder, int position) {
        // 进行网络页面控制的请求
        if (result != null) {
            FoodShowResult.ResultEntity resultEntity = result.get(position);

            holder.nameTv.setText(resultEntity.getUser_name());

            holder.levelTv.setText(resultEntity.getLevel_title());
            holder.commentTv.setText(resultEntity.getComment());
            holder.placenameTv.setText(resultEntity.getPlace_name());
            holder.foodnameTv.setText(resultEntity.getFood_name());
            holder.addressTv.setText(resultEntity.getAddress());
            holder.phoneTv.setText(resultEntity.getPhone());

            // 将获取的日期(2014-06-17 13:20:10)切割,分别进行赋值
            String[] date =resultEntity.getPublish_time().split("\\ ");
            if (date != null && date.length > 0) {

            holder.timeTv.setText(date[1]);
            holder.dateTv.setText(date[0]);
            }



            if (resultEntity.getUser_image() != null
                    && resultEntity.getUser_image().length() > 0) {
                String nameImgUrl = resultEntity.getUser_image();

               GetVolleyDatasUtil.getBitmapRequest(nameImgUrl, holder.headIv);
            }
            if (resultEntity.getPicture_url() != null
                    && resultEntity.getPicture_url().length() > 0) {
                String foodImagUrl = resultEntity.getPicture_url();
                getBitmap(foodImagUrl,holder.foodIv);
 //                 holder.foodIv.setImageUrl(foodImagUrl,imageLoader);


            }

        }
    }



    private void getBitmap(String url, final NetworkImageView imageView) {
        ImageLoader imageLoader = VolleySingleton.getInstance().getImageLoader();
        imageView.setDefaultImageResId(R.mipmap.ic_launcher);
        imageView.setErrorImageResId(R.mipmap.ic_launcher);
        imageView.setImageUrl(url, imageLoader);

    }

    @Override
    public FoodShowViewHolder onCreateViewHolder(ViewGroup parent, int position) {
        LayoutInflater inflater = LayoutInflater.from(
                parent.getContext());
        View v =inflater.inflate(R.layout.food_show_item,null);

        return new FoodShowViewHolder(v);
    }


}
